﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevOnServer
{
    public class CMoverProp
    {
        public int ID;

        public short Size = 100;

        public short Speed = 100;

        public int HP = 300;

        public bool Flying = false;

        public float AttackFirstRange = 8.0f;

        public int AttackDelay = 5500;

        public int AtkMin = 15;

        public int AtkMax = 16;

        public long Exp = 25;
    }

    public partial class CDataManager
    {
        public Dictionary<int, CMoverProp> MoverProperties = new Dictionary<int, CMoverProp>();

        private bool _loadPropMover(string filename)
        {
            CFileReader file = new CFileReader();
            if (!file.load(filename))
            {
                CConsole.WriteLine("Can't open mover prop file '{0}'", filename);
                return false;
            }

            CMoverProp prop;

            string tok;
            while (!file.end)
            {
                tok = file.getString();
                if (!m_defines.ContainsKey(tok))
                    continue;

                prop = new CMoverProp();

                prop.ID = m_defines[tok];

                file.move(19);

                prop.AtkMin = file.getInt();
                if (prop.AtkMin == -1)
                    prop.AtkMin = 0;

                prop.AtkMax = file.getInt();
                if (prop.AtkMax == -1)
                    prop.AtkMax = 0;

                file.move(12);

                prop.AttackDelay = file.getInt();
                if (prop.AttackDelay == -1)
                    prop.AttackDelay = 5500;

                prop.HP = file.getInt();

                file.move(8);

                prop.Speed = (short)(file.getFloat() * 1000.0f);

                if (prop.Speed <= 0)
                    prop.Speed = 100;

                file.move(1);

                prop.Flying = file.getBool();

                file.move(15);

                prop.Exp = file.getLong();
                if (prop.Exp == -1)
                    prop.Exp = 0;

                file.move(20);

                MoverProperties[prop.ID] = prop;
            }

            return true;
        }

        private bool _loadMdlDyna(string filename)
        {
            CFileReader file = new CFileReader();
            if (!file.load(filename))
            {
                CConsole.WriteLine("Can't open model file '{0}'", filename);
                return false;
            }

            string tok;
            while (!file.end)
            {
                tok = file.getString();
                if (tok != "mvr")
                    continue;

                file.move(2);

                while (!file.end)
                {
                    tok = file.getString().ToLower();
                    if (tok == "npc")
                    {
                        while (tok != "}")
                        {
                            while (tok != "}")
                                tok = file.getString();
                            tok = file.getString();
                        }
                    }
                    else if (tok == "monster")
                    {
                        file.move(1);

                        _readMdlDynaGroup(file);
                    }
                }
            }

            return true;
        }

        private void _readMdlDynaGroup(CFileReader file)
        {
            string tok;
            while (!file.end)
            {
                tok = file.getString();

                if (tok == "}")
                {
                    return;
                }
                else
                {
                    tok = file.getString();
                    if (m_defines.ContainsKey(tok))
                    {
                        file.move(5);
                        int id = m_defines[tok];
                        if (MoverProperties.ContainsKey(id))
                        {
                            MoverProperties[id].Size = (short)(file.getFloat() * 100f);
                        }
                        else
                            file.move(1);

                        file.move(5);

                        while (tok != "}" && !file.end)
                            tok = file.getString();
                    }
                    else if (tok == "{")
                    {
                        _readMdlDynaGroup(file);
                    }
                }
            }
        }

        private bool _loadPropMoverEx(string filename)
        {
            CFileReader file = new CFileReader();
            if (!file.load(filename))
            {
                CConsole.WriteLine("Can't open moverEx prop file '{0}'", filename);
                return false;
            }

            CMoverProp prop;

            string tok;
            while (!file.end)
            {
                tok = file.getString();
                if (!m_defines.ContainsKey(tok))
                    continue;

                prop = MoverProperties[m_defines[tok]];

                tok = file.getString();
                while (tok != "}")
                {
                    switch (tok)
                    {
                        case "m_nAttackFirstRange":
                            file.move(1); // =
                            prop.AttackFirstRange = (float)int.Parse(file.getString().Replace(';', ' '));
                            break;
                    }

                    tok = file.getString();
                }
            }

            return true;
        }
    }
}